package zl.osimall.front.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zl.osimall.common.bean.User;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.service.UserService;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String toLogin(){
        return "user/login";
    }

    @GetMapping("/reg")
    public String toRog(){
        return "user/reg";
    }

    @PostMapping("/login")
    public String login(String username, String password, HttpSession session, Model m){
        try {
            User user = userService.login(username, password);
            session.setAttribute("user", user);
            return "redirect:/index";
        } catch (SteduException e) {
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/login";
        }

    }

    @PostMapping("/reg")
    public String reg(String username, String password,String repassword, HttpSession session, Model m){
        if(!password.equals(repassword)){
            SteduException e = new SteduException("两次密码不相同");
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/reg";
        }
        try {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            userService.register(user);
            return "user/login";
        } catch (SteduException e) {
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/reg";
        }

    }

    @GetMapping("/update1")
    public String toUpdate1(HttpSession session){
        return "user/userUpdate1";
    }

    @PostMapping("/update1")
    public String update1(String username, String password,String newPassword1,String newPassword2, HttpSession session, Model m){
        User u = (User) session.getAttribute("user");
        if(!newPassword1.equals(newPassword2) ){
            SteduException e = new SteduException("两次密码不相同w");
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/userUpdate1";
        }
        try {
            User user = new User();
            user.setUsername(username);
            user.setPassword(newPassword1);
            userService.update(user,password );
            session.invalidate();
            return "user/login";
        } catch (SteduException e) {
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/userUpdate1";
        }
    }

    @GetMapping("/update2")
    public String toUpdate2(HttpSession session){
        return "user/userUpdate2";
    }

    @PostMapping("/update2")
    public String update2(String username, String password,String payPassword,String newPayPassword, HttpSession session, Model m){
        User u = (User) session.getAttribute("user");
        if(!payPassword.equals(newPayPassword) ){
            SteduException e = new SteduException("两次密码相同");
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/userUpdate2";
        }
        try {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setPayPassword(payPassword);
            userService.updatePayPassword(user, newPayPassword);
            session.invalidate();
            return "user/login";
        } catch (SteduException e) {
            e.printStackTrace();
            m.addAttribute("errMsg", e.getMessage());
            return "user/userUpdate2";
        }
    }


    @GetMapping("/person")
    public String person(HttpSession session, Model m){
        User user = (User) session.getAttribute("user");
        System.out.println(user.toString());
        m.addAttribute("user", user);
        return "user/person";
    }

    @PostMapping("/person")
    public String updatePerson(User user, HttpSession session, Model m){
        User before = (User)session.getAttribute("user");
        System.out.println(user.toString());
        if(!before.getUsername().equals(user.getUsername())){
            m.addAttribute("errMsg", "非法用户名");
            return "user/person";
        }
        userService.update(user);
        try {
            User after = userService.selectByUsername(user.getUsername());
            session.setAttribute("user", after);
            m.addAttribute("user", after);
        } catch (SteduException e) {
            throw new RuntimeException(e);
        }
        m.addAttribute("errMsg", "修改成功");
        return "/user/person";
    }


    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }


}
